package flc.ast.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.TransferTabAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferTabBean;
import flc.ast.databinding.ActivityFileTransferBinding;
import flc.ast.fragment.AppFragment;
import flc.ast.fragment.AudioFragment;
import flc.ast.fragment.PictureFragment;
import flc.ast.fragment.VideoFragment;
import flc.ast.utils.AndroidFileServer;
import flc.ast.utils.Content;
import flc.ast.utils.SendFileManager;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;

public class FileTransferActivity extends BaseAc<ActivityFileTransferBinding> {

    public static int type;
    private List<Fragment> mFragmentList;
    private TransferTabAdapter mTabAdapter;
    private int oldPosition;
    private AndroidFileServer server;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_transfer;
    }

    @Override
    protected void initView() {
        mDataBinding.ivFileTransferBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivConfirm.setOnClickListener(this);

        mFragmentList = new ArrayList<>();
        mFragmentList.add(new PictureFragment());
        mFragmentList.add(new VideoFragment());
        mFragmentList.add(new AudioFragment());
        mFragmentList.add(new AppFragment());
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flFragment, 0);

        mDataBinding.rvTransferFileTab.setLayoutManager(new GridLayoutManager(mContext, 4));
        mTabAdapter = new TransferTabAdapter();
        mDataBinding.rvTransferFileTab.setAdapter(mTabAdapter);
        mTabAdapter.setOnItemClickListener(this);

        if (type == 0) {
            server = new AndroidFileServer(this, Content.Port);
            try {
                server.start();
            } catch (IOException e) {

            }
        }
    }

    @Override
    protected void onClickCallback(View v) {
        if (v.getId() == R.id.ivConfirm) {
            if (SendFileManager.getInstance().getSelectFileBeans().size() == 0) {
                ToastUtils.showShort(R.string.no_choose_file_transfer_tips);
            } else {
                List<Transferable> transferableList = new ArrayList<>();
                List<ContactInfo> contactInfoList = new ArrayList<>();
                List<File> selectedFiles = new ArrayList<>();
                for (SelectFileBean fileBean : SendFileManager.getInstance().getSelectFileBeans()) {
                    selectedFiles.add(new File(fileBean.getPath()));
                    switch (fileBean.getType()) {
                        case 1://图片
                            FileInfo picFile = new FileInfo();
                            picFile.setFileName(fileBean.getName());
                            picFile.setUriStr(fileBean.getPath());
                            picFile.setFileType(FileType.IMAGE);
                            picFile.setSize(fileBean.getSize());
                            transferableList.add(picFile);
                            break;
                        case 2://视频
                            FileInfo videoFile = new FileInfo();
                            videoFile.setFileName(fileBean.getName());
                            videoFile.setUriStr(fileBean.getPath());
                            videoFile.setFileType(FileType.VIDEO);
                            videoFile.setSize(fileBean.getSize());
                            transferableList.add(videoFile);
                            break;
                        case 3://音频
                            FileInfo audioFile = new FileInfo();
                            audioFile.setFileName(fileBean.getName());
                            audioFile.setUriStr(fileBean.getPath());
                            audioFile.setFileType(FileType.AUDIO);
                            audioFile.setSize(fileBean.getSize());
                            transferableList.add(audioFile);
                            break;
                        case 4://应用
                            FileInfo appFile = new FileInfo();
                            appFile.setFileName(fileBean.getName());
                            appFile.setUriStr(fileBean.getPath());
                            appFile.setFileType(FileType.APK);
                            appFile.setSize(fileBean.getSize());
                            transferableList.add(appFile);
                            break;
                        case 5://联系人
                            ContactInfo contactInfo = new ContactInfo();
                            contactInfo.setName(fileBean.getName());
                            contactInfo.setPhone(fileBean.getPath());
                            contactInfoList.add(contactInfo);
                            TfContactInfo tfContactInfo = new TfContactInfo(contactInfoList);
                            transferableList.add(tfContactInfo);
                            break;
                        default:
                            break;
                    }
                }
                if (type == 0) {
                    startActivity(MutualTransferActivity.class);
                } else {
                    TransferableSendManager.getInstance().setTransferables(transferableList);
                    startActivity(FileSendShowQrActivity.class);
                }
            }
        }
    }


    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mTabAdapter.getItem(oldPosition).setSelected(false);
        mTabAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        mTabAdapter.getItem(position).setSelected(true);
        mTabAdapter.notifyItemChanged(position);
        FragmentUtils.showHide(position, mFragmentList);
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));

        List<TransferTabBean> listTab = new ArrayList<>();
        listTab.add(new TransferTabBean(R.drawable.atpp, R.drawable.atpan2, true));
        listTab.add(new TransferTabBean(R.drawable.asp, R.drawable.asp1, false));
        listTab.add(new TransferTabBean(R.drawable.ayp, R.drawable.yp, false));
        listTab.add(new TransferTabBean(R.drawable.ayy, R.drawable.yy, false));
        mTabAdapter.setList(listTab);
    }

    public void refreshDataCount(SendFileManager sendFileManager) {
        List<SelectFileBean> selectFileBeans = sendFileManager.getSelectFileBeans();
        if (CollectionUtils.isEmpty(selectFileBeans)) {
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        } else {
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, selectFileBeans.size()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!SendFileManager.getInstance().getSelectFileBeans().isEmpty()) {
            SendFileManager.getInstance().clearSelectFile();
        }
        if (server != null) {
            server.stop();
        }
    }

}